<template>
  <HomePanel title="热门品牌" sub-title="国际经典 品质保证">
    <template v-slot:right>
      <!-- 设置点击参数 -->
      <a href="javascript:;" class="iconfont icon-angle-left prev" :class='{disabled: index === 0}' @click='toggle(0)'></a>
      <a href="javascript:;" class="iconfont icon-angle-right next" :class='{disabled: index === 1}' @click='toggle(1)'></a>
    </template>
    <div class="box" ref="target">
      <!-- 动态修改样式 通过平移实现滑动展示 -->
      <!-- 通过索引 控制平移实现轮播效果 -->
      <ul v-if='brands.length' class="list" :style='{transform: `translateX(${-index * 1238}px)`}'>
        <li v-for="item in brands" :key="item.id">
          <RouterLink to="/">
            <img :src="item.picture" alt="">
          </RouterLink>
        </li>
      </ul>
      <div v-else class="skeleton">
        <!-- 使用骨架插件 -->
        <XtxSkeleton class="item" v-for="i in 5" :key="i" animated bg="#e4e4e4" width="240px" height="305px" />
      </div>
    </div>
  </HomePanel>
</template>

<script>
// 导入调用插槽的模板
import HomePanel from './home-pannel'
// 导入获取展示数据的api接口
import { findBrand } from '@/api/home.js'
import { ref } from 'vue'
// 导入懒加载组件
import useLazyData from '@/hooks'
export default {
  name: 'HomeBrand',
  components: { HomePanel },
  setup () {
    // 控制滑动的状态位
    const index = ref(0)
    // const brands = ref([])
    // findBrand().then(data => {
    //   brands.value = data.result
    // })
    // 导入图片懒加载插件
    // 懒加载需要传递参数 先通过函数方法调用接口获取promise数据 到达指容器位置后 再传递服务器获取的数据
    const { target, result } = useLazyData(() => findBrand(10))
    // 控制左右切换
    const toggle = (i) => {
      // 左侧按钮参数是0 右侧按钮参数是1
      index.value = i
    }
    return { target, brands: result, index, toggle }
  }
}
</script>

<style scoped lang='less'>
.home-panel {
  background: #f5f5f5;
}
.iconfont {
  width: 20px;
  height: 20px;
  background: #ccc;
  color: #fff;
  display: inline-block;
  text-align: center;
  margin-left: 5px;
  background: @xtxColor;
  &::before {
    font-size: 12px;
    position: relative;
    top: -2px;
  }
  &.disabled {
    background: #ccc;
    cursor: not-allowed;
  }
}
.box {
  display: flex;
  width: 100%;
  height: 345px;
  overflow: hidden;
  padding-bottom: 40px;
  // position: relative;
  .list {
    width: 200%;
    display: flex;
    transition: all 1s;
    li {
      margin-right: 10px;
      width: 240px;
      img {
        width: 240px;
        height: 305px;
      }
    }
  }
  // 单独定制骨架效果
  .skeleton {
    width: 100%;
    display: flex;
    .item {
      margin-right: 10px;
      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
}
</style>
